package com.netease.xone.activity;

import preferMgr.XonePrefHelper;
import protocol.XoneService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.fragment.FragmentGuide;
import com.netease.xone.fragment.FragmentTouTiao;

import db.manager.ManagerAccount;
import db.manager.ManagerAccount.Account;

public class ActivityWelcome extends ActivityX1Base {
    

    public static void launch(Context context, int mode){
        Intent intent = new Intent(context, ActivityWelcome.class);
        //非activity的context启动activity会出错
        if(!(context instanceof Activity))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(XoneConsts.BUNDLE_KEY.TOUTIAO_MODE, mode);
        context.startActivity(intent);
    }

    private int mMode;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMode = FragmentTouTiao.MODE_INVALID;
        if (null != getIntent()) {
            mMode = getIntent().getIntExtra(XoneConsts.BUNDLE_KEY.TOUTIAO_MODE, FragmentTouTiao.MODE_INVALID);
        }
        
        FrameLayout linear = new FrameLayout(this);
        linear.setId(R.id.activity_welcome_container_id);
        setContentView(linear);
        
        if (null != findViewById(R.id.activity_welcome_container_id) && null == savedInstanceState) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = null;

            if (XonePrefHelper.getFirstUse(getApplicationContext())) {
                /**
                 * 首次使用，进入向导
                 */
                fragment = FragmentGuide.newInstance(null);
            } else {
                /**
                 * 非首次使用，进入头条
                 */
                fragment = FragmentTouTiao.newInstance(mMode);
            }
            ft.replace(R.id.activity_welcome_container_id, fragment, null);
            ft.commit();
        }
        
        Account acc = ManagerAccount.getInstance().getCurrentAccount();
        if(acc != null) {
            XoneService.getInstance().doLogin(acc.mUserType, acc.mUserName, acc.mPassword);
        }else{
            XoneService.getInstance().doAnomyousLogin();
        }
    }

    @Override
    protected void onResume() {
        if (mMode == FragmentTouTiao.MODE_FINISH) {
            getWindow().setWindowAnimations(R.style.window_anim_slide);
        }
        super.onResume();
    }
}
